package com.dylan.equipment.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dylan.equipment.entity.Department;
import com.dylan.equipment.entity.PageBean;
import com.dylan.equipment.entity.User;
import com.dylan.equipment.service.DepartmentService;
import com.dylan.equipment.service.UserService;
import com.dylan.equipment.utils.PageUtil;
import com.dylan.equipment.utils.ResponseUtil;
import com.dylan.equipment.utils.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/department")
public class DepartmentAction {

	@Resource
	private UserService userService;
	@Resource
	private DepartmentService departmentService;
	
	
	@RequestMapping("list")
	public ModelAndView list (@RequestParam(value="page" ,required=false)String page,Department s_department,HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		HttpSession session=request.getSession();
		//查询前台是否提交了页码
		if(StringUtil.isEmpty(page)){
			page="1";
		}
		//判断页面是否提交了模糊查询
		if(s_department!=null){
			session.setAttribute("s_department", s_department);
		}
		//封装页码信息
		PageBean pageBean=new PageBean(Integer.parseInt(page),10);
		//分页查询加载部门信息
		List<Department> departmentList = departmentService.find(pageBean, s_department);
		//统计信息的总条数
		int count = departmentService.count(s_department);
		//页码导航
		String pageCode=PageUtil.getPagation(request.getContextPath()+"/department/list.do", count, Integer.parseInt(page), 10);
		//添加页码导航代码
		mav.addObject("pageCode", pageCode);
		//添加model显示名称
		mav.addObject("modeName", "部门管理");
		//添加部门列表
		mav.addObject("departmentList", departmentList);
		//添加部门列表页面地址
		mav.addObject("mainPage", "department/list.jsp");
		//添加跳转的页面
		mav.setViewName("main");
		return mav;
	}
	
	@RequestMapping("delete")
	public void delete(@RequestParam(value="id")String id ,HttpServletResponse response) throws Exception{
		JSONObject result=new JSONObject();
		//判断该部门下面是否存在用户信息
		if(userService.existUserByDeptId(Integer.parseInt(id))){
			result.put("errorInfo", "该部门下存在用户，不能删除！");
		}else{
			//没有用户信息的时候直接删除
			departmentService.delete(Integer.parseInt(id));
			result.put("success", true);			
		}
		ResponseUtil.write(result, response);
	}
	
	@RequestMapping("/preSave")
	public ModelAndView preSave(@RequestParam(value="id",required=false)String id){
		ModelAndView mav=new ModelAndView();
		//在main中添加显示页面
		mav.addObject("mainPage", "department/save.jsp");
		//页面显示的名称
		mav.addObject("modeName", "部门管理");
		//跳转的页面
		mav.setViewName("main");
		//判断页面是否提交了id，有id责表示修改，无责添加
		if(StringUtil.isNotEmpty(id)){
			mav.addObject("actionName", "部门修改");
			Department department=departmentService.loadById(Integer.parseInt(id));
			mav.addObject("department", department);
		}else{
			mav.addObject("actionName", "部门添加");			
		}
		return mav;
	}
	@RequestMapping("/save")
	public String save(Department department){
		//判断id是否为空，为空就添加
		if(department.getId()==null){
			departmentService.add(department);			
		}else{
			departmentService.update(department);
		}
		//跳转至list
		return "redirect:/department/list.do";
	}
}
